//
//  BannerView.m
//  ZhiBo
//
//  Created by 苏贵明 on 16/3/7.
//  Copyright © 2016年 苏贵明. All rights reserved.
//

#import "BannerView.h"
#import "homePageModel.h"

@implementation BannerView{

    UIScrollView* scroll;
    NSTimer* timer;
    int page;
    
    UIPageControl* pageControl;
    UIImage *image;
    
    NSMutableArray* dataArray;
}
@synthesize tapBlock;

-(instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)array{
    self = [super initWithFrame:frame];
    if (self) {
        
        dataArray = [[NSMutableArray alloc]init];
        [dataArray addObjectsFromArray:array];
        
        scroll = [[UIScrollView alloc]initWithFrame:self.bounds];
        scroll.delegate = self;
        [scroll setContentSize:CGSizeMake(self.bounds.size.width*(dataArray.count + 1), self.bounds.size.height)];
        scroll.pagingEnabled = YES;
        scroll.showsHorizontalScrollIndicator = NO;
        [scroll setContentOffset:CGPointMake(self.bounds.size.width, 0) animated:NO];
        [self addSubview:scroll];
        // 轮播图数量+1
        for (int i = 0; i < dataArray.count; i++) {
            if (i == dataArray.count) {
                // 最后一张同第一张
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH * dataArray.count, 0, SCREENWIDTH, self.bounds.size.height)];
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                imageView.userInteractionEnabled = YES;
                UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTap)];
                [imageView addGestureRecognizer:tap];
                [imageView sd_setImageWithURL:[NSURL URLWithString:dataArray[i]]];
                
                [scroll addSubview:imageView];
    
            }else{
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREENWIDTH, 0, SCREENWIDTH, self.bounds.size.height)];
                imageView.contentMode = UIViewContentModeScaleAspectFill;
                imageView.clipsToBounds = YES;
                imageView.userInteractionEnabled = YES;
                UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTap)];
                [imageView addGestureRecognizer:tap];
                [imageView sd_setImageWithURL:[NSURL URLWithString:dataArray[i]]];
               
                [scroll addSubview:imageView];
            }
            
        }
//        scroll.contentOffset = CGPointMake(screenWidth, 0);
        pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-20, self.bounds.size.width, 20)];
        pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
        pageControl.numberOfPages = dataArray.count;
        
        pageControl.currentPage = 0;
        
        [pageControl addTarget:self action:@selector(valueChanged:) forControlEvents:(UIControlEventValueChanged)];
        
        [self addSubview:pageControl];
        
       timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];

    }
    return self;
}


- (void)autoPlay
{
    CGFloat x = scroll.contentOffset.x;
    
    CGFloat width = scroll.frame.size.width;
    
    if (width + x >= scroll.contentSize.width) {
        
        [scroll setContentOffset:CGPointMake(0, 0) animated:NO];
        
        [scroll setContentOffset:CGPointMake(width, 0) animated:YES];
        
    }else{
        [scroll setContentOffset:CGPointMake(x + width, 0) animated:YES];
    }
}
- (void)valueChanged:(UIPageControl *)pageControl1
{
    NSInteger page1 = pageControl1.currentPage;
    
    CGFloat x = (page1 + 1) * scroll.frame.size.width;
    
    [scroll setContentOffset:CGPointMake(x, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    
    CGFloat width =  scrollView.frame.size.width;
    
    if (x > dataArray.count * width) {
        [scroll setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    if (x < 0) {
        [scroll setContentOffset:CGPointMake(dataArray.count * width, 0) animated:NO];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    CGFloat x = scroll.contentOffset.x;
    
    page = x / scrollView.frame.size.width - 1;
    
    
    if (page < 0) {
        pageControl.currentPage = dataArray.count;
        page = (int)dataArray.count - 1;
    }else{
        pageControl.currentPage = page;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = scroll.contentOffset.x;
    
    page = x / scrollView.frame.size.width - 1;
    
    if (page < 0) {
        pageControl.currentPage = dataArray.count;
        page = (int)dataArray.count - 1;
    }else{
        pageControl.currentPage = page;
    }
}

-(void)imageTap{
    tapBlock(page);
}
#pragma mark - scroll view delegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self pauseTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self startTimerAfterTimeInterval:2];
}

#pragma mark - timer

-(void)pauseTimer
{
    if (![timer isValid]) {
        return ;
    }
    [timer setFireDate:[NSDate distantFuture]];
}

-(void)startTimer
{
    if (![timer isValid]) {
        return ;
    }
    [timer setFireDate:[NSDate date]];
}

- (void)startTimerAfterTimeInterval:(NSTimeInterval)interval
{
    if (![timer isValid]) {
        return ;
    }
    [timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}

@end
